<?php


/**
 * xhr autocomplete answer class
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @version 1
 * @package core
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * initialize the frontend
 *
 * @package cmsrfe
 * @version 3
 */
class autocomplete
{
	/**
	 * @var object instance
	 */
	private static $INSTANCE;
    /**
     * @var object answer
     */
    private $ANSWER;
	/**
	 * instantiate essential objects and make them avail. as subobjects
	 *
	 * @return	void
	 */
	private function __construct()
    {
        // init result
        $this->ANSWER = new stdclass();
        #$this->ANSWER->Results = new stdclass();
	}
	/**
	 * return a instance of object
	 *
	 * @return object instance
	 */
	public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c;
        }
        return self::$INSTANCE;
    }
    /**
     * return xhr string
     *
     * @return string
     */
    public function __toString()
    {
        $json = new Services_JSON();
        return $json->encode($this->ANSWER);
    }
    /**
     * add a result field and id
     *
     * @param string result fieldname
     * @param string result field id (optional)
     * @return bool
     */
    public function addResult ($field, $fieldid = null)
    {
        $this->ANSWER->Results[] = array ('field' => $field, 'id' => $fieldid);
        return true;
    }
}